import { create } from "zustand";
import { persist } from "zustand/middleware";
import { bannerGet } from "../api/course";
import { BannerType } from "../type/course";
interface BannerStateType {
  banner: Array<BannerType>;
  fetchBanner: () => void;
  addBanner: (obj: BannerType) => void;
}
export const useBanner = create<BannerStateType>()(
  persist(
    (set, get) => ({
      banner: [],
      async fetchBanner() {
        let res = await bannerGet();
        set(() => ({
          banner: res.data.results,
        }));
      },
      addBanner(obj) {
        let banner = get().banner; //get获取到的是当前状态机模块对象
        banner.push(obj);
        set({ banner });
      },
    }),
    {
      name: "banner-list", //本地存储配置
    }
  )
);
